//
//  AddressSelectView.m
//  AppointmentForCar
//
//  Created by TheMacBook on 2016/11/15.
//  Copyright © 2016年 Herron. All rights reserved.
//

#import "AddressSelectView.h"

@implementation AddressSelectView {
    AddressView *_address;
    UIButton *_nowCallButton;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        self.backgroundColor = [UIColor clearColor];
        _address = [[AddressView alloc] init];
        _address.delegate = self;
        self.addSubview(_address);
    }
    return self;
}

- (void)setup {
    __weak typeof(self) weakSelf = self;
    if(_isDestination) {
        _nowCallButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _nowCallButton.backgroundColor = [UIColor colorWithString:@"#EE7D00"];
        [_nowCallButton setTitle:@"立刻呼叫" forState:UIControlStateNormal];
        [_nowCallButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_nowCallButton addTarget:self action:@selector(clickNowCall) forControlEvents:UIControlEventTouchUpInside];
        _nowCallButton.layer.cornerRadius = 5;
        self.addSubview(_nowCallButton);
        [_address mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.mas_top);
            make.left.equalTo(weakSelf.mas_left).mas_offset(kScaleWidth(8));
            make.right.equalTo(weakSelf.mas_right).mas_offset(-kScaleWidth(8));
            make.bottom.equalTo(_nowCallButton.mas_top).mas_offset(-kScaleHeight(6));
        }];
        [_nowCallButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.mas_left).mas_offset(kScaleWidth(8));
            make.right.equalTo(weakSelf.mas_right).mas_offset(-kScaleWidth(8));
            make.bottom.equalTo(weakSelf.mas_bottom).mas_offset(-kScaleHeight(10));
            make.height.mas_equalTo(kScaleHeight(44));
        }];
    } else {
        if(_nowCallButton) {
            [_nowCallButton removeFromSuperview];
        }
        [_address mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.mas_top);
            make.left.equalTo(weakSelf.mas_left).mas_offset(kScaleWidth(8));
            make.right.equalTo(weakSelf.mas_right).mas_offset(-kScaleWidth(8));
            make.bottom.equalTo(weakSelf.mas_bottom).mas_offset(-kScaleHeight(10));
        }];
    }
}

- (void)addressViewClickDestination:(AddressView *)view {
    if([_delegate respondsToSelector:@selector(addressSelectClickDestination:)]) {
        [_delegate addressSelectClickDestination:self];
    }
}

- (void)addressViewClickOrigin:(AddressView *)view {
    if([_delegate respondsToSelector:@selector(addressSelectClickOrigin:)]) {
        [_delegate addressSelectClickOrigin:self];
    }
}

- (void)addressNowCallCar:(AddressView *)view {
    if([_delegate respondsToSelector:@selector(addressSelectNowCallCar:)]) {
        [_delegate addressSelectNowCallCar:self];
    }
}

- (void)clickNowCall {
    if([_delegate respondsToSelector:@selector(addressSelectNowCallCar:)]) {
        [_delegate addressSelectNowCallCar:self];
    }
}

-(void)setIsDestination:(BOOL)isDestination {
    _isDestination = isDestination;
    _address.isDestination = isDestination;
    [self setup];
}

- (void)setOrigin:(NSString *)origin {
    _origin = origin;
    _address.origin = origin;
}

- (void)setDestination:(NSString *)destination {
    _destination = destination;
    _address.destination = destination;
}

@end

@implementation AddressView {
    UIButton *_nowStartButton;
    AddressItem *_originItem;
    AddressItem *_destinationItem;
    UIView *_line;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = 5;
        self.layer.borderWidth = 1;
        self.layer.borderColor = [UIColor colorWithString:@"#cccccc"].CGColor;
        self.layer.masksToBounds = YES;
        _originItem = [[AddressItem alloc] init];
        _originItem.delegate = self;
        _originItem.type = 1;
        _line = [UIView new];
        _line.backgroundColor = [UIColor colorWithString:@"#cccccc120"];
        _destinationItem = [[AddressItem alloc] init];
        _destinationItem.delegate = self;
        _destinationItem.type = 2;
        self.addSubview(_originItem).addSubview(_destinationItem).addSubview(_line);
    }
    return self;
}

- (void)setup {
    __weak typeof(self) weakSelf = self;
    if(_isDestination) {
        _nowStartButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _nowStartButton.enabled = NO;
        [_nowStartButton setTitle:@"现在出发" forState:UIControlStateNormal];
        [_nowStartButton setTitleColor:[UIColor colorWithString:@"#EE7D00"] forState:UIControlStateNormal];
        [_nowStartButton addTarget:self action:@selector(nowCall) forControlEvents:UIControlEventTouchUpInside];
        _nowStartButton.backgroundColor = [UIColor colorWithString:@"#F8F8F8"];
        self.addSubview(_nowStartButton);
        [_originItem mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(kScaleHeight(44));
            make.top.left.right.equalTo(weakSelf);
        }];
        [_line mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.mas_left).mas_offset(kScaleWidth(10));
            make.right.equalTo(weakSelf.mas_right).mas_offset(-kScaleWidth(10));
            make.height.mas_equalTo(kScaleHeight(1));
            make.top.equalTo(_originItem.mas_bottom);
            make.bottom.equalTo(_destinationItem.mas_top);
        }];
        [_destinationItem mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf);
            make.height.mas_equalTo(_originItem.mas_height);
            make.bottom.equalTo(_nowStartButton.mas_top);
        }];
        [_nowStartButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(weakSelf);
            make.height.mas_equalTo(kScaleHeight(44));
        }];
    } else {
        if(_nowStartButton) {
            [_nowStartButton removeFromSuperview];
        }
        [_originItem mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.height.mas_equalTo(kScaleHeight(50));
            make.top.left.right.equalTo(weakSelf);
        }];
        [_line mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.mas_left).mas_offset(kScaleWidth(10));
            make.right.equalTo(weakSelf.mas_right).mas_offset(-kScaleWidth(10));
            make.height.mas_equalTo(kScaleHeight(1));
            make.top.equalTo(_originItem.mas_bottom);
        }];
        [_destinationItem mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.equalTo(weakSelf);
            make.height.mas_equalTo(_originItem.mas_height);
            make.top.equalTo(_line.mas_bottom);
        }];
    }
}

- (void)addressDestinationClick:(AddressItem *)item {
    if([_delegate respondsToSelector:@selector(addressViewClickDestination:)]) {
        [_delegate addressViewClickDestination:self];
    }
}

- (void)addressOriginClick:(AddressItem *)item {
    if([_delegate respondsToSelector:@selector(addressViewClickOrigin:)]) {
        [_delegate addressViewClickOrigin:self];
    }
}

- (void)nowCall {
    if([_delegate respondsToSelector:@selector(addressNowCallCar:)]) {
        [_delegate addressNowCallCar:self];
    }
}

- (void)setIsDestination:(BOOL)isDestination {
    _isDestination = isDestination;
    [self setup];
}

- (void)setOrigin:(NSString *)origin {
    _origin = origin;
    _originItem.content = origin;
}

- (void)setDestination:(NSString *)destination {
    _destination = destination;
    _destinationItem.content = destination;
}

@end

@implementation AddressItem {
    UIImageView *_image;
    UILabel *_contentLabel;
}

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if(self) {
        self.backgroundColor = [UIColor whiteColor];
        _image = [[UIImageView alloc] init];
        _contentLabel = [[UILabel alloc] init];
        self.addSubview(_image).addSubview(_contentLabel);
        _contentLabel.font = kAppFontSize(13);
        __weak typeof(self) weakSelf = self;
        [_image mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(weakSelf.mas_left).mas_offset(kScaleWidth(12));
            make.height.mas_lessThanOrEqualTo(kScaleHeight(20));
            make.width.mas_lessThanOrEqualTo(kScaleWidth(20));
            make.centerY.equalTo(weakSelf);
        }];
        [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(weakSelf.mas_top).mas_offset(kScaleHeight(10));
            make.bottom.equalTo(weakSelf.mas_bottom).mas_offset(-kScaleHeight(10));
            make.left.equalTo(_image.mas_right).mas_offset(kScaleWidth(15));
            make.right.equalTo(weakSelf.mas_right).mas_offset(-kScaleWidth(8));
        }];
        _contentLabel.userInteractionEnabled = YES;
        UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(clickItem)];
        [_contentLabel addGestureRecognizer:tap];
    }
    return self;
}

- (void)clickItem {
    if (1 == _type) {
        if([_delegate respondsToSelector:@selector(addressOriginClick:)]) {
            [_delegate addressOriginClick:self];
        }
    } else {
        if([_delegate respondsToSelector:@selector(addressDestinationClick:)]) {
            [_delegate addressDestinationClick:self];
        }
    }
}

- (void)setType:(NSInteger)type {
    _type = type;
    if(type == 1) {
        _image.image = [UIImage imageNamed:@"callStart"];
        _contentLabel.textColor = [UIColor blackColor];
    } else {
        _image.image = [UIImage imageNamed:@"callEnd"];
        _contentLabel.textColor = [UIColor colorWithString:@"#EE7D00"];
        _contentLabel.text = @"您要去哪儿";
    }
}

- (void)setContent:(NSString *)content {
    _content = content;
    _contentLabel.text = content;
}

@end
